package com.ruoyi.web.controller.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.module.active.CommunityActive;
import com.ruoyi.system.domain.module.asset.EstateAsset;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.estate.EstateBuildFloor;
import com.ruoyi.system.domain.module.estate.EstateRoom;
import com.ruoyi.system.domain.module.estate.EstateRoomAccountRecord;
import com.ruoyi.system.domain.module.event.EventCleanArea;
import com.ruoyi.system.domain.module.event.EventPatrolPoint;
import com.ruoyi.system.domain.module.event.EventPointQr;
import com.ruoyi.system.domain.module.event.EventTerminals;
import com.ruoyi.system.domain.module.mer.Mer;
import com.ruoyi.system.domain.module.product.Product;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.domain.module.property.PropertyFeeOrder;
import com.ruoyi.system.domain.module.property.PropertyFeeOrderInfo;
import com.ruoyi.system.domain.module.setMeal.SetMealQrcode;
import com.ruoyi.system.domain.module.survey.Survey;
import com.ruoyi.system.service.module.active.ICommunityActiveService;
import com.ruoyi.system.service.module.asset.IEstateAssetService;
import com.ruoyi.system.service.module.commonInfo.ICommonInfoImgService;
import com.ruoyi.system.service.module.estate.IEstateBuildFloorService;
import com.ruoyi.system.service.module.estate.IEstateRoomAccountRecordService;
import com.ruoyi.system.service.module.estate.IEstateRoomService;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.event.IEventCleanAreaService;
import com.ruoyi.system.service.module.event.IEventPatrolPointService;
import com.ruoyi.system.service.module.event.IEventPointQrService;
import com.ruoyi.system.service.module.event.IEventTerminalsService;
import com.ruoyi.system.service.module.mer.IMerService;
import com.ruoyi.system.service.module.product.IProductService;
import com.ruoyi.system.service.module.property.IPropertyFeeOrderInfoService;
import com.ruoyi.system.service.module.property.IPropertyFeeOrderService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.module.setMeal.ISetMealQrcodeService;
import com.ruoyi.system.service.module.survey.ISurveyService;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@RestController
public class CommonController {

	private static final Logger log = LoggerFactory.getLogger(CommonController.class);

	@Resource
	private IEstateService estateService;
	@Resource
	private IEstateRoomService estateRoomService;
	@Resource
	private IProductService productService;
	@Resource
	private IMerService merService;
	@Resource
	private IEventTerminalsService terminalsService;
	@Resource
	private ISetMealQrcodeService mealQrCodeService;
	@Resource
	private IPropertyFeeOrderService feeOrderService;
	@Resource
	private IPropertyService propertyService;
	@Resource
	private IPropertyFeeOrderInfoService feeOrderInfoService;
	@Resource
	private IEventPatrolPointService patrolPointService;
	@Resource
	private IEventCleanAreaService cleanAreaService;
	@Resource
	private IEstateBuildFloorService buildFloorService;
	@Resource
	private ICommunityActiveService communityActiveService;
	@Resource
	private IEventPointQrService eventPointQrService;
	@Resource
	private ISurveyService surveyService;
	@Resource
	private IEstateRoomAccountRecordService estateRoomAccountRecordService;
	@Resource
	private IEstateAssetService estateAssetService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;
	@Value("${ruoyi.profile}")
	private String profile;


	/**
	 * 下载图片素材
	 */
	@Log(title = "下载图片素材")
	@GetMapping("common/download/child")
	public void downloadChild(HttpServletResponse response) {

		String assetId = "809d9b47347a6af2860e7a3a35bce057"; //jsonObject.getStr("assetId");
		String downloadUrl = "https://cdn.assets.scratch.mit.edu/internalapi/asset/" + assetId + ".svg/get/";
		FileUtils.downloadFile1(downloadUrl, profile + "/backdrops/", assetId + ".svg");


//		File file = new File(profile + "/costumes.json");
//		String jsonStr = FileUtils.getStr(file);
//		if(StrUtil.isNotEmpty(jsonStr)){
//			JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
//			if(jsonArray!= null){
//				for (int i =0;i<jsonArray.size();i++){
//					JSONObject jsonObject = (JSONObject)jsonArray.get(i);
//					String assetId = jsonObject.getStr("assetId");
//					String downloadUrl = "https://cdn.assets.scratch.mit.edu/internalapi/asset/"+assetId+".svg/get";
//					FileUtils.downloadFile(downloadUrl,profile + "/backdrops/" + assetId+".svg");
//				}
//			}
//		}
	}

	/**
	 * 生成SCHEME跳转连接
	 *
	 * @param code 编码
	 */
	@GetMapping("s/{code}")
	public void fileDownload(@PathVariable("code") String code, HttpServletResponse response, HttpServletRequest request) {

	}

	/**
	 * 通用下载请求
	 *
	 * @param fileName 文件名称
	 * @param delete   是否删除
	 */
	@GetMapping("common/download")
	public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
		try {
			if (!FileUtils.checkAllowDownload(fileName)) {
				throw new Exception(StrUtil.format("文件名称({})非法，不允许下载。 ", fileName));
			}
			String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
			String filePath = RuoYiConfig.getDownloadPath() + fileName;
			File file = new File(filePath);
			response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
			FileUtils.setAttachmentResponseHeader(response, realFileName);
			FileUtils.writeToStream(file, response.getOutputStream());
			if (delete) {
				FileUtils.del(file);
			}
		} catch (Exception e) {
			log.error("下载文件失败", e);
		}
	}

	/**
	 * 本地资源通用下载
	 */
	@GetMapping("/common/download/resource")
	public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			if (!FileUtils.checkAllowDownload(resource)) {
				throw new Exception(StrUtil.format("资源文件({})非法，不允许下载。 ", resource));
			}
			// 本地资源路径
			String localPath = RuoYiConfig.getProfile();
			// 数据库资源地址
			String downloadPath = localPath + StrUtil.subAfter(resource, Constants.RESOURCE_PREFIX, false);
			// 下载名称
			String downloadName = StrUtil.subAfter(downloadPath, "/", true);
			response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
			File file = new File(downloadPath);
			FileUtils.setAttachmentResponseHeader(response, downloadName);
			FileUtils.writeToStream(file, response.getOutputStream());
		} catch (Exception e) {
			log.error("下载文件失败", e);
		}
	}


	/**
	 * 调查问卷
	 */
	@Log(title = "下载调查问卷二维码")
	@GetMapping("common/download/survey/{id}")
	public void downloadSurvey(@PathVariable("id") Integer id, HttpServletResponse response) {
		Survey survey = surveyService.getById(id);
		if (survey != null) {
			try {
				String fileName = survey.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 下载小区二维码
	 */
	@Log(title = "下载终端设备二维码")
	@GetMapping("common/download/estate/{id}")
	public void downloadEstate(@PathVariable("id") Integer id, HttpServletResponse response) {
		Estate estate = estateService.getById(id);
		if (estate != null) {
			try {
				String fileName = estate.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载小区房屋二维码
	 */
	@Log(title = "下载终端设备二维码")
	@GetMapping("common/download/room/{id}")
	public void downloadEstateRoom(@PathVariable("id") Integer id, HttpServletResponse response) {
		EstateRoom estateRoom = estateRoomService.getById(id);
		if (estateRoom != null) {
			try {
				String fileName = estateRoom.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 下载终端设备二维码
	 */
	@Log(title = "下载巡检设备二维码")
	@GetMapping("common/download/terminal/{id}")
	public void downloadTerminal(@PathVariable("id") Integer id, HttpServletResponse response) {
		EventTerminals terminals = terminalsService.getById(id);
		if (terminals != null) {
			try {
				String fileName = terminals.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 巡更点
	 */
	@Log(title = "下载巡更点二维码")
	@GetMapping("common/download/patrol/{id}")
	public void downloadPatrol(@PathVariable("id") Integer id, HttpServletResponse response) {
		EventPatrolPoint point = patrolPointService.getById(id);
		if (point != null) {
			try {
				String fileName = point.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 区域二维码
	 */
	@Log(title = "下载区域二维码")
	@GetMapping("common/download/area/{id}")
	public void downloadCleanArea(@PathVariable("id") Integer id, HttpServletResponse response) {
		EventCleanArea area = cleanAreaService.getById(id);
		if (area != null) {
			try {
				String fileName = area.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 楼层二维码
	 */
	@Log(title = "下载楼层二维码")
	@GetMapping("common/download/floor/{id}")
	public void downloadFloor(@PathVariable("id") Integer id, HttpServletResponse response) {
		EstateBuildFloor floor = buildFloorService.getById(id);
		if (floor != null) {
			try {
				String fileName = floor.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 地点二维码
	 */
	@Log(title = "下载地点二维码")
	@GetMapping("common/download/event/pointQr/{id}")
	public void downloadEventPointQr(@PathVariable("id") Integer id, HttpServletResponse response) {
		EventPointQr pointQr = eventPointQrService.getById(id);
		if (pointQr != null) {
			try {
				String fileName = pointQr.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}



	/**
	 * 下载商品二维码
	 */
	@Log(title = "下载商品二维码")
	@GetMapping("common/download/product/{id}")
	public void downloadProduct(@PathVariable("id") Integer id, HttpServletResponse response) {
		Product product = productService.getById(id);
		if (product != null) {
			try {
				String fileName = product.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载商品二维码
	 */
	@Log(title = "下载商家二维码")
	@GetMapping("common/download/mer/{id}")
	public void downloadMer(@PathVariable("id") Integer id, HttpServletResponse response) {
		Mer mer = merService.getById(id);
		if (mer != null) {
			try {
				String fileName = mer.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载套餐活动二维码
	 */
	@Log(title = "下载商家二维码")
	@GetMapping("common/download/meal/{id}")
	public void downloadMeal(@PathVariable("id") Integer id, HttpServletResponse response) {
		SetMealQrcode setMeal = mealQrCodeService.getById(id);
		if (setMeal != null) {
			try {
				String fileName = setMeal.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载活动二维码
	 */
	@Log(title = "下载商家二维码")
	@GetMapping("common/download/communityActive/{id}")
	public void downloadcommunityActive(@PathVariable("id") Integer id, HttpServletResponse response) {
		CommunityActive action = communityActiveService.getById(id);
		if (action != null) {
			try {
				String fileName = action.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载固定设备码
	 */
	@Log(title = "下载商家二维码")
	@GetMapping("common/download/gudingAsset/{id}")
	public void downloadgudingAsset(@PathVariable("id") Integer id, HttpServletResponse response) {
		EstateAsset asset = estateAssetService.getById(id);
		if (asset != null) {
			try {
				String fileName = asset.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载固定设备码
	 */
	@Log(title = "下载商家二维码")
	@GetMapping("common/download/yidongAsset/{id}")
	public void downloadyidongAsset(@PathVariable("id") Integer id, HttpServletResponse response) {
		EstateAsset asset = estateAssetService.getById(id);
		if (asset != null) {
			try {
				String fileName = asset.getQrCode();
				String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
				String filePath = profile + "/" + fileName;

				response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
				File file = new File(filePath);
				FileUtils.setAttachmentResponseHeader(response, realFileName);
				FileUtils.writeToStream(file, response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 下载物业对账单
	 */
	@Log(title = "下载物业对账单")
	@GetMapping("common/download/settle/{id}")
	public void downloadSettle(@PathVariable("id") Integer id, HttpServletResponse response) {
		try {
			String sou = String.valueOf(System.currentTimeMillis());

			PropertyFeeOrder propertyFeeOrder = feeOrderService.findById(id);
			String tempFileName = "收据.docx";
			Property property = propertyService.findById(propertyFeeOrder.getPropertyId());
			if (property != null) {
				tempFileName = property.getTemplateFileName();
			}

			String path = profile + File.separator + "file" + File.separator + "temp" + File.separator + tempFileName;
			System.out.println("文件路径 :" + path);

			File template = new File(path);
			InputStream ins = new FileInputStream(template);

			IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
			//创建xdocreport上下文对象
			IContext context = report.createContext();

//			if(feeOrder!=null){
//				QueryWrapper<PropertyFeeOrder> qw = new QueryWrapper<>();
////				qw.eq("region_id",feeOrder.getRegionId());
//				qw.eq("begin_date",feeOrder.getBeginDate());
//				qw.eq("end_date",feeOrder.getEndDate());
//				PropertyFeeOrder propertyFeeOrder = feeOrderService.getOne(qw);
//				if(propertyFeeOrder!=null){
//				}
//			}

			// 封装数据 // 季度
			context = setWordValue(context, property, propertyFeeOrder);
			//创建字段元数据
			String fileName = profile + File.separator + "file" + File.separator + DateUtils.getMonthStr(new Date()) + File.separator + "物业费收据_" + sou + ".docx";
			File filPath = new File(profile + File.separator + "file" + File.separator + DateUtils.getMonthStr(new Date()));
			if (!filPath.exists()) {
				filPath.mkdir();
			}
			System.out.println("fileName: " + fileName);
			File file = new File(fileName);
			FileOutputStream out = new FileOutputStream(file);
			report.process(context, out);
			out.flush();
			out.close();
			ins.close();

			String pdfFilePath = WordToPDF.wordConverterToPdf(fileName);
			System.out.println("pdfFilePath:  " + pdfFilePath);

			String imgPdfPath = "file" + File.separator + DateUtils.getMonthStr(new Date()) + File.separator;
			System.out.println("imgPdfPath :  " + imgPdfPath);

			String imgUrl = WordToPDF.pdfFileToImages(pdfFilePath, profile, imgPdfPath, propertyFeeOrder.getOrderNo());

			propertyFeeOrder.setReceiptImgUrl(imgUrl);

			//生成的 pdf文件
			String pdfPath = File.separator + "file" + File.separator + DateUtils.getMonthStr(new Date()) + File.separator + "物业费收据_" + sou + ".pdf";
			propertyFeeOrder.setReceiptFilePath(pdfPath);
			feeOrderService.updateById(propertyFeeOrder);
			try {
				if (file != null) {
//					String filePath = fileName;// profile + "/file/" + fileName;
					response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
					FileUtils.setAttachmentResponseHeader(response, file.getName());
					FileUtils.writeToStream(file, response.getOutputStream());

					InputStream inputStream = new FileInputStream(new File(fileName));
					OssUtil.uploadObs(inputStream, DateUtils.getMonthStr(new Date()) + "/物业费收据_" + sou + ".pdf",15);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * word模板赋值
	 *
	 * @param context
	 */
	private IContext setWordValue(IContext context, Property property, PropertyFeeOrder propertyFeeOrder) {
//		context.put("regionName", regionName);
//		context.put("billName", billName);
//		context.put("beginDate", propertyFeeOrder.getBeginDate() );
//		context.put("endDate", propertyFeeOrder.getEndDate() );
//		context.put("settleNo", propertyFeeOrder.getSettlementNo());
//		context.put("splitMoney", propertyFeeOrder.getSplitMoney().toString());
//		context.put("outRuleMoney", propertyFeeOrder.getOutRuleMoney().toString());
//		context.put("paySplitMoney", propertyFeeOrder.getPaySplitMoney().toString());
//		context.put("orderCount", propertyFeeOrder.getOrderCount().toString());

		context.put("companyName", "精彩e家");
		if (property != null) {
			context.put("companyName", property.getCompanyName());
		}
		if (propertyFeeOrder.getRemark()!=null){
			context.put("remark",propertyFeeOrder.getRemark());
		}else {
			context.put("remark","");
		}
		context.put("estateName",propertyFeeOrder.getEstateName());
		if (propertyFeeOrder.getPayPlatform()==0){
			context.put("rechargWay","微信支付");
		}else if (propertyFeeOrder.getPayPlatform()==1){
			context.put("rechargWay","银联支付");
		}else if (propertyFeeOrder.getPayPlatform()==2){
			context.put("rechargWay","现金支付");
		}else if (propertyFeeOrder.getPayPlatform()==3){
			context.put("rechargWay","微信支付");
		}else if (propertyFeeOrder.getPayPlatform()==4){
			context.put("rechargWay","刷卡支付");
		}else if (propertyFeeOrder.getPayPlatform()==5){
			context.put("rechargWay","支付宝转账");
		}else if (propertyFeeOrder.getPayPlatform()==6){
			context.put("rechargWay","预存抵扣");
		}else if (propertyFeeOrder.getPayPlatform()==7){
			context.put("rechargWay","转账支付");
		}else if (propertyFeeOrder.getPayPlatform()==8){
			context.put("rechargWay","多种付款");
		}else if (propertyFeeOrder.getPayPlatform()==10){
			context.put("rechargWay","押金支付");
		}

		context.put("realName", propertyFeeOrder.getRealName());
		context.put("nickName", propertyFeeOrder.getNickName());
		context.put("roomNo", "");
		if (propertyFeeOrder.getRoomNo() != null) {
			context.put("roomNo", propertyFeeOrder.getRoomNo());
		}
		context.put("payTime", DateUtils.getSdfTimesCN1(propertyFeeOrder.getCreateTime()));
		context.put("no", String.format("%010d", propertyFeeOrder.getId()));

		//水费
		//起止数
		context.put("wa_s_e", "");
		//水费数量
		context.put("wa_n", "");
		//水费单价
		context.put("wa_p", "");

		context.put("wa_h", "");
		PropertyFeeOrderInfo waterFee = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(), "water");
		if (waterFee != null) {
			if (waterFee.getBeginCount() != null && waterFee.getEndCount() != null) {
				context.put("wa_s_e", waterFee.getBeginCount() + "~" + waterFee.getEndCount());
			}
			if (waterFee.getCount() != null) {
				context.put("wa_n", waterFee.getCount() + "m³");
			}
			if (propertyFeeOrder.getWaterSinglePrice() != null) {
				context.put("wa_p", propertyFeeOrder.getWaterSinglePrice());
			}
		}

		context.put("wa_w", "");
		context.put("wa_q", "");
		context.put("wa_b", "");
		context.put("wa_s", "");
		context.put("wa_y", "");
		context.put("wa_j", "");
		context.put("wa_f", "");
		if (propertyFeeOrder.getWaterFee() != null && propertyFeeOrder.getWaterFee().doubleValue() > 0) {
			String[] waterPrice = StringUtil.yuanToArray(propertyFeeOrder.getWaterFee().doubleValue());
			String[] waPirce = {"wa_w", "wa_q", "wa_b", "wa_s", "wa_y", "wa_j", "wa_f"};
			int i_water_price = 0;
			for (String s : waterPrice) {
				context.put(waPirce[i_water_price], s);
				i_water_price++;
			}
		}

		//加压费
		context.put("jy_s_e", "");
		context.put("jy_n", "");
		context.put("jy_p", "");
		context.put("jy_h", "");
//		PropertyFeeOrderInfo jiayaFee = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(),"jiaya");
//		if(jiayaFee != null){
//			if(jiayaFee.getBeginCount()!=null && jiayaFee.getEndCount()!=null){
//				context.put("jy_s_e", jiayaFee.getBeginCount() + "~" + jiayaFee.getEndCount());
//			}
//			if(jiayaFee.getCount()!=null){
//				context.put("jy_n", jiayaFee.getCount());
//			}
//			if(jiayaFee.getSinglePrice()!=null){
//				context.put("jy_p", jiayaFee.getSinglePrice());
//			}
//		}
		if (waterFee != null && waterFee.getAttachPrice().doubleValue()>0) {
			if (waterFee.getBeginCount() != null && waterFee.getEndCount() != null) {
				context.put("jy_s_e", waterFee.getBeginCount() + "~" + waterFee.getEndCount());
			}
			if (waterFee.getCount() != null) {
				context.put("jy_n", waterFee.getCount() + "m³");
			}
			if (waterFee.getAttachSinglePrice() != null && waterFee.getAttachSinglePrice().doubleValue() > 0) {
				context.put("jy_p", waterFee.getAttachSinglePrice());
			}
		}


		context.put("jy_w", "");
		context.put("jy_q", "");
		context.put("jy_b", "");
		context.put("jy_s", "");
		context.put("jy_y", "");
		context.put("jy_j", "");
		context.put("jy_f", "");
		if (propertyFeeOrder.getJiaYaFee() != null && propertyFeeOrder.getJiaYaFee().doubleValue() > 0) {
			String[] jiayaPrice = StringUtil.yuanToArray(propertyFeeOrder.getJiaYaFee().doubleValue());
			String[] jyPirce = {"jy_w", "jy_q", "jy_b", "jy_s", "jy_y", "jy_j", "jy_f"};
			int i_jiaya_price = 0;
			for (String s : jiayaPrice) {
				context.put(jyPirce[i_jiaya_price], s);
				i_jiaya_price++;
			}
		}


		//电费
		context.put("el_s_e", "");
		context.put("el_n", "");
		context.put("el_p", "");
		context.put("el_h", "");

		PropertyFeeOrderInfo eletrictFee = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(), "eletrict");
		if (eletrictFee != null) {
			if (eletrictFee.getBeginCount() != null && eletrictFee.getEndCount() != null) {
				String elSe = eletrictFee.getBeginCount() + "~" + eletrictFee.getEndCount();
				context.put("el_s_e", elSe);
			}
			if (eletrictFee.getCount() != null) {
				context.put("el_n", eletrictFee.getCount() + "度电");
			}
			if (propertyFeeOrder.getElectricSinglePrice() != null) {
				context.put("el_p", propertyFeeOrder.getElectricSinglePrice());
			}
		}


		context.put("el_w", "");
		context.put("el_q", "");
		context.put("el_b", "");
		context.put("el_s", "");
		context.put("el_y", "");
		context.put("el_j", "");
		context.put("el_f", "");

		if (propertyFeeOrder.getElectricFee() != null && propertyFeeOrder.getElectricFee().doubleValue() > 0) {
			String[] electricPrice = StringUtil.yuanToArray(propertyFeeOrder.getElectricFee().doubleValue());
			String[] elePirce = {"el_w", "el_q", "el_b", "el_s", "el_y", "el_j", "el_f"};
			int i_ele_price = 0;
			for (String s : electricPrice) {
				context.put(elePirce[i_ele_price], s);
				i_ele_price++;
			}
		}

		//物业费
		context.put("wyf_s_e", "");//起止数
		context.put("wyf_n", "");//数量
		context.put("wyf_p", "");//单价
		context.put("wyf_h", "");//优惠

		PropertyFeeOrderInfo wyfFee = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(), "wyf");
		if (wyfFee != null) {
			if (wyfFee.getBeginDate() != null && wyfFee.getEndDate() != null) {
				context.put("wyf_s_e", DateUtils.getSimpleYearMonthTime(wyfFee.getBeginDate()) + "~" + DateUtils.getSimpleYearMonthTime(wyfFee.getEndDate()));
			}
			if (propertyFeeOrder.getRoomAreas() != null) {
				context.put("wyf_n", propertyFeeOrder.getRoomAreas() + "㎡");
			}
			if (propertyFeeOrder.getWyfSinglePrice() != null) {
				context.put("wyf_p", propertyFeeOrder.getWyfSinglePrice());
			}
			if (propertyFeeOrder.getWyfYhPrice() != null) {
				context.put("wyf_h", propertyFeeOrder.getWyfYhPrice());
			}
		}


		context.put("wyf_w", "");
		context.put("wyf_q", "");
		context.put("wyf_b", "");
		context.put("wyf_s", "");
		context.put("wyf_y", "");
		context.put("wyf_j", "");
		context.put("wyf_f", "");

		if (propertyFeeOrder.getWyfFee() != null && propertyFeeOrder.getWyfFee().doubleValue() > 0) {
			String[] wyfPrice = StringUtil.yuanToArray(propertyFeeOrder.getWyfFee().doubleValue());
			String[] wfyPriceTxt = {"wyf_w", "wyf_q", "wyf_b", "wyf_s", "wyf_y", "wyf_j", "wyf_f"};
			int i_wyf_price = 0;
			for (String s : wyfPrice) {
				context.put(wfyPriceTxt[i_wyf_price], s);
				i_wyf_price++;
			}
		}

		//垃圾处理费
		context.put("ru_s_e", "");//起止数
		context.put("ru_n", "");//数量
		context.put("ru_p", "");//单价
		context.put("ru_h", "");//优惠

//		PropertyFeeOrderInfo rubbishFee = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(),"rubbish");
//		if(rubbishFee != null){
//			if(rubbishFee.getCount()!=null){
//				context.put("ru_n", rubbishFee.getMonthCount()+"个月");
//			}
//			context.put("ru_p", "6.6");
//		}
		if (wyfFee != null && wyfFee.getAttachPrice()!=null) {
			if(wyfFee.getAttachPrice().doubleValue()>0){
				if (wyfFee.getBeginDate() != null && wyfFee.getEndDate() != null) {
					context.put("ru_s_e", DateUtils.getSimpleYearMonthTime(wyfFee.getBeginDate()) + "~" + DateUtils.getSimpleYearMonthTime(wyfFee.getEndDate()));
				}
				if (wyfFee.getAttachPrice() != null && wyfFee.getAttachPrice().doubleValue() > 0) {
					context.put("ru_n", wyfFee.getMonthCount() + "个月");
				}
				if (wyfFee.getAttachSinglePrice() != null && wyfFee.getAttachSinglePrice().doubleValue() > 0) {
					context.put("ru_p", wyfFee.getAttachSinglePrice());
				}
				if (wyfFee.getAttachYouhuiPrice() != null && wyfFee.getAttachYouhuiPrice().doubleValue() > 0) {
					context.put("ru_h", wyfFee.getAttachYouhuiPrice());
				}
			}
		}


		context.put("ru_w", "");
		context.put("ru_q", "");
		context.put("ru_b", "");
		context.put("ru_s", "");
		context.put("ru_y", "");
		context.put("ru_j", "");
		context.put("ru_f", "");

		if (propertyFeeOrder.getTrashFee() != null && propertyFeeOrder.getTrashFee().doubleValue() > 0) {
			String[] rubbishPrice = StringUtil.yuanToArray(propertyFeeOrder.getTrashFee().doubleValue());
			String[] rubbishPriceTxt = {"ru_w", "ru_q", "ru_b", "ru_s", "ru_y", "ru_j", "ru_f"};
			int i_rubbish_price = 0;
			for (String s : rubbishPrice) {
				context.put(rubbishPriceTxt[i_rubbish_price], s);
				i_rubbish_price++;
			}
		}

		//车位费
		context.put("car_s_e", "");
		context.put("car_n", "");
		context.put("car_p", "");
		context.put("car_h", "");
		PropertyFeeOrderInfo carFee = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(), "car");
		if (carFee != null) {
			if (carFee.getCount() != null) {
				context.put("car_n", carFee.getCount());
			}
			if (propertyFeeOrder.getCarSinglePrice() != null) {
				context.put("car_p", propertyFeeOrder.getCarSinglePrice());
			}
		}


		context.put("car_w", "");
		context.put("car_q", "");
		context.put("car_b", "");
		context.put("car_s", "");
		context.put("car_y", "");
		context.put("car_j", "");
		context.put("car_f", "");
		if (propertyFeeOrder.getCarFee() != null && propertyFeeOrder.getCarFee().doubleValue() > 0) {
			String[] carPrice = StringUtil.yuanToArray(propertyFeeOrder.getCarFee().doubleValue());
			String[] carPriceTxt = {"car_w", "car_q", "car_b", "car_s", "car_y", "car_j", "car_f"};
			int i_car_price = 0;
			for (String s : carPrice) {
				context.put(carPriceTxt[i_car_price], s);
				i_car_price++;
			}
		}

		//套内账户
		context.put("ac_c", "");
		context.put("ac_b", "");
		context.put("ac_y", "");

		if (propertyFeeOrder.getAccountOrderId()!=null){
			EstateRoomAccountRecord record = estateRoomAccountRecordService.findById(propertyFeeOrder.getAccountOrderId());
			if (record!=null){
				if (propertyFeeOrder.getAccountFee() != null) {
					context.put("ac_b", propertyFeeOrder.getAccountFee());
				}
				if (record.getRoomId()!=null){
					EstateRoom room = estateRoomService.findById(record.getRoomId());
					context.put("ac_y", room.getAccount());
					if (record.getRechargeOrUse()==0){
						Double num = room.getAccount()-propertyFeeOrder.getAccountFee();
						context.put("ac_c", num.toString());
					}else {
						Double num =room.getAccount()+propertyFeeOrder.getAccountFee();
						context.put("ac_c", num.toString());
					}
				}

			}
		}


		PropertyFeeOrderInfo account = feeOrderInfoService.getOrderInfoByType(propertyFeeOrder.getId(), "account");
		if (account != null) {
			if (account.getTotalPrice() != null) {
				context.put("ac_b", account.getTotalPrice());
			}

		}

		//总额 大写
		context.put("money_daxie", MathUtil.moneyToBigMoney(propertyFeeOrder.getPayMoney().doubleValue()));

		//合计总和 小写
		context.put("to_w", "");
		context.put("to_q", "");
		context.put("to_b", "");
		context.put("to_s", "");
		context.put("to_y", "");
		context.put("to_j", "");
		context.put("to_f", "");
		if (propertyFeeOrder.getTotalMoney() != null && propertyFeeOrder.getPayMoney().doubleValue() > 0) {
			String[] totalPrice = StringUtil.yuanToArray(propertyFeeOrder.getPayMoney().doubleValue());
			String[] totalPriceTxt = {"to_w", "to_q", "to_b", "to_s", "to_y", "to_j", "to_f"};
			int i_total_price = 0;
			for (String s : totalPrice) {
				context.put(totalPriceTxt[i_total_price], s);
				i_total_price++;
			}
		}
		return context;
	}

	/**
	 * 通用上传请求
	 */
	@PostMapping("/common/upload/{uploadType}")
	public AjaxResult uploadFile(@PathVariable(value = "uploadType") Integer uploadType, MultipartFile file) throws Exception {
		try {
			// 上传并返回新文件名称
			String fileName = FileUploadUtils.upload(profile, file, uploadType);
			//图片网络路径
			String url = imgUrl + fileName;
			Map<String, Object> ajax = new HashMap<>();
			ajax.put("fileName", fileName);
			ajax.put("url", url);
			return AjaxResult.success(ajax);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error(e.getMessage());
		}
	}

	/**
	 * APP版本更新上传文件
	 */
	@PostMapping("/common/uploadAppVersion/{version}")
	public AjaxResult appVersionUpload(@PathVariable(value = "version") String appVersion, MultipartFile file) throws Exception {
		try {
			// 上传并返回新文件名称
			String fileName = FileUploadUtils.uploadAppVersion(profile, file, appVersion);
			//图片网络路径
			String url = imgUrl + fileName;
			Map<String, Object> ajax = new HashMap<>();
			ajax.put("fileName", fileName);
			ajax.put("url", url);
			return AjaxResult.success(ajax);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error(e.getMessage());
		}
	}
}
